package cn.agiledata.bank.myspace.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.dao.TransactionTemplate;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.common.util.UniqueIdentityUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.myspace.bean.AccountHisQueryDetailBean;
import cn.agiledata.bank.myspace.bean.PaperBean;
import cn.agiledata.bank.myspace.service.IMySpaceService;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class PrintPaperAction extends IPBBaseAction implements Constant {
	public ActionForward getPrintPaper(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IPBException, Exception {
		
		
		String transId = (String)request.getParameter("transId");
		
		PaperBean bean = getPrintPaperBean(transId, String.valueOf(WeatherUtil.getAccountIdFromSession(request)));
		
		
		request.setAttribute("bean", bean);
		
		String forwardName = getForward(bean);
		
		return mapping.findForward(forwardName);
	}
	
	/**
	 * 得到打印列表
	 * @param transId
	 * @param accountId
	 * @return
	 */
	public PaperBean getPrintPaperBean(String transId,String accountId) {
		IMySpaceService mySpaceService = (IMySpaceService) IPBAppFactory.getObj("mySpaceService");
		PaperBean aqb = mySpaceService.getPaper(transId, accountId);
		if(aqb.getVoucherCodeShow().equals(NOT_EXIST_VOUCHER_CODE)) {	//不存在回单号,则添加回单号
			paperSave(aqb.getTid());
		}
		return aqb;
	}
	
	/**
	 * 回单保存
	 * @param id
	 */
	public void paperSave(String id) {
		TransactionTemplate tt = (TransactionTemplate) IPBAppFactory
		.getObj("transactionTemplate");
		Transaction tran = tt.getTransaction(id);
		UniqueIdentityUtil uiu = new UniqueIdentityUtil();
		if(String.valueOf(tran.getActionType().getId()).equals(Constant.ACTION_TYPE_C)) {	//账户间交易的回单生成
			tran.setVoucherCode(uiu.getTransferPaperSequence());
		}		
		if(String.valueOf(tran.getActionType().getId()).equals(Constant.ACTION_TYPE_A) || 
		   String.valueOf(tran.getActionType().getId()).equals(Constant.ACTION_TYPE_B) ||
		   String.valueOf(tran.getActionType().getId()).equals(Constant.ACTION_TYPE_F) ||
		   String.valueOf(tran.getActionType().getId()).equals(Constant.ACTION_TYPE_K) ||
		   String.valueOf(tran.getActionType().getId()).equals(Constant.ACTION_TYPE_L) ||
		   String.valueOf(tran.getActionType().getId()).equals(Constant.ACTION_TYPE_O) ||
		   String.valueOf(tran.getActionType().getId()).equals(Constant.ACTION_TYPE_P) ||
		   String.valueOf(tran.getActionType().getId()).equals(Constant.ACTION_TYPE_Q)) {	//B2C网上银行充值,线下充值,B2C网上银行消费,B2C电话银行充值,B2C电话银行消费,B2B网上银行充值,B2B网上银行消费,卡消费的回单生成
			tran.setVoucherCode(uiu.getReceivePaperSequence());
		}
		if(String.valueOf(tran.getActionType().getId()).equals(Constant.ACTION_TYPE_D) || String.valueOf(tran.getActionType().getId()).equals(Constant.ACTION_TYPE_G)) {	//提现和账户付款给对方银行的回单生成
			tran.setVoucherCode(uiu.getPayPaperSequence());
		}
		tt.updateTran(tran);
	}
	
	/**
	 * 根据不同的交易类型,forward到不同的页面
	 * @param list
	 * @return
	 */
	public String getForward(AccountHisQueryDetailBean bean) {
		String forwardName = "";
		String actionType = bean.getActionType(); //交易类型
		if(actionType.equals(ACTION_TYPE_C)) {	//账户间交易是支付系统内划转单
			forwardName = "transferPaper";
		}
		if(actionType.equals(ACTION_TYPE_D) || actionType.equals(ACTION_TYPE_G))	{	//提现，支付账户付款给对方银行
			forwardName = "payPaper";
		}
		if(actionType.equals(Constant.ACTION_TYPE_B)) {	//线下充值的回单生成
			forwardName = "receivePaper";
		}
		if(actionType.equals(Constant.ACTION_TYPE_A) ||
		   actionType.equals(Constant.ACTION_TYPE_F) ||
		   actionType.equals(Constant.ACTION_TYPE_K) ||
		   actionType.equals(Constant.ACTION_TYPE_L) ||
		   actionType.equals(Constant.ACTION_TYPE_O) ||
		   actionType.equals(Constant.ACTION_TYPE_P) ||
		   actionType.equals(Constant.ACTION_TYPE_Q)) {	//B2C网上银行充值,B2C网上银行消费,B2C电话银行充值,B2C电话银行消费,B2B网上银行充值,B2B网上银行消费,卡消费的回单生成
			forwardName = "receiveOnlinePaper";
		}
			
		return forwardName;
	}
	

}
